#!/bin/bash
# 该脚本适用于前后端分离部署模式
set -x

# 环境变量示例
##export ENV=dev
##export PLATFORM=linkapp
##export SERVICE_COMPONENT=backend
##export MAVEN_TARGET=linkapp-api-1.0.0-SNAPSHOT.jar

# 环境
ENV="${ENV:-unknown}"
# 平台
PLATFORM="${PLATFORM:-unknown}"
# 服务组件
SERVICE_COMPONENT="${SERVICE_COMPONENT:-unknown}"
# 启动用户
APP_USER="${PLATFORM}-${SERVICE_COMPONENT}"
case "${APP_USER}" in
    linkapp-backend)
      APP_USER_ID=3301
      ;;
    *)
      echo "Unknown ID for ${APP_USER}"
      exit 10
      ;;
esac

# 启动用户组
APP_GROUP="${APP_USER}"
APP_GROUP_ID="${APP_USER_ID}"
# 应用标识
APP_MARK="${PLATFORM}-${SERVICE_COMPONENT}_4_${ENV}"
# maven构建产出物
MAVEN_TARGET="${MAVEN_TARGET:-unknown}"
# web应用包
WEBAPP_TARGET="${WEBAPP_TARGET:-ROOT}"
# 守护进程名
SERVICE_NAME="springboot-${APP_MARK}"
# 本次构建标识
DATE_SUFFIX="$(date +%Y%m%d.%H%M%S.%N)"

# 应用根目录
APP_ROOT=/elk/app/springboot
APP_LOG_ROOT=/elk/log/java

if [ $ENV == "unknown" -o $PLATFORM == "unknown" -o $SERVICE_COMPONENT == "unknown" -o $MAVEN_TARGET == "unknown" ]; then
  echo "Warnings: 必填参数 ENV, PLATFORM, SERVICE_COMPONENT, MAVEN_TARGET 丢失。"
  exit 1
fi

# 检查用户合法性
check_run_user() {
  RUN_USER=$1
  RUN_USER_ID=$2
  RUN_GROUP=$3
  RUN_GROUP_ID=$4
  BELONG_GROUP="$5"

  id ${RUN_USER} > /dev/null 2>&1
  retval=$?
  if [ ! $retval == 0 ]; then
    echo "Warnings: 运行用户 ${RUN_USER} 缺失。"
    exit 91
  fi

  id ${RUN_GROUP} > /dev/null 2>&1
  retval=$?
  if [ ! $retval == 0 ]; then
    echo "Warnings: 运行用户组 ${RUN_GROUP} 缺失。"
    exit 92
  fi
  
  REAL_RUN_USER_ID=`id -u ${RUN_USER}`
  REAL_RUN_GROUP_ID=`id -g ${RUN_USER}`
  
  if [ ! "${RUN_USER_ID}" == "${REAL_RUN_USER_ID}" ]; then
    echo "Warning: 运行用户 ${RUN_USER} 的 ID ${REAL_RUN_USER_ID} 非法，期望值是 ${RUN_USER_ID}"
    exit 93
  fi
  
  if [ ! "${RUN_GROUP_ID}" == "${REAL_RUN_GROUP_ID}" ]; then
    echo "Warning: 运行用户组 ${RUN_GROUP} 的 ID ${REAL_RUN_GROUP_ID} 非法，期望值是 ${RUN_GROUP_ID}"
    exit 94
  fi

  REAL_BELONG_GROUP="`groups ${RUN_USER} | cut -d':' -f 2`"
  if [ ! -z "${BELONG_GROUP}" ]; then
    for grp in ${BELONG_GROUP}
    do
      grp_exist=no
      for r_grp in ${REAL_BELONG_GROUP}
      do
        if [ ${grp} == ${r_grp} ]; then
          grp_exist=yes
        fi
      done
      if [ "${grp_exist}" == "no" ]; then
        echo "Warning: 用户 ${RUN_USER} 的所属组 ${REAL_BELONG_GROUP} 非法，期望值是 ${BELONG_GROUP}"
        exit 95
      fi
    done
  fi
}

check_run_user java 2024 java 2024
check_run_user ${APP_USER} ${APP_USER_ID} ${APP_GROUP} ${APP_GROUP_ID} "java"

echo 1.准备相关目录结构
# 带日期后缀的【日志】根目录不再每次部署时重新创建
mkdir -pv ${APP_ROOT}/${APP_MARK}-$DATE_SUFFIX
mkdir -pv ${APP_LOG_ROOT}/${APP_MARK}

echo 2.更新后端软链接
cd ${APP_ROOT}
[ -L ${APP_MARK} ] && rm -f ${APP_MARK}
ln -svf ${APP_MARK}-$DATE_SUFFIX ${APP_MARK}

echo 3.放置后端java程序
cd /elk/inst/other/${APP_MARK}
tar xf package.tar.gz ${MAVEN_TARGET}
mv ${MAVEN_TARGET} ${APP_ROOT}/${APP_MARK}-$DATE_SUFFIX/${PLATFORM}-${SERVICE_COMPONENT}.jar

echo 5.启动前配置
cd ${APP_ROOT}/${APP_MARK}

echo 6.自助式授权
chown -R ${APP_USER}:${APP_GROUP}     \
${APP_ROOT}/${APP_MARK}-$DATE_SUFFIX  \
${APP_ROOT}/${APP_MARK}               \
${APP_LOG_ROOT}/${APP_MARK}

echo 7.重启Java应用（Java应用的更新需要重启后生效）
systemctl restart ${SERVICE_NAME}
#sleep 5
#systemctl start ${SERVICE_NAME}
